﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Koala.Pro.CodeGenerator.UI.Controls
{
    public interface IProgressCallback
    {
        bool IsAborting { get; }

        void Begin(int minimum, int maximum);

        void Begin(ProgressAutoCloseTypes autoClose, int minimum, int maximum);

        void Begin();

        void SetAutoClose(ProgressAutoCloseTypes autoClose);

        void SetRange(int minimum, int maximum);

        void SetText(string text1, string text2);

        void SetText(string text);

        void AddMessage(string message);

        void AddMessage(ProgressMessageTypes type, string message);

        void StepTo(int val);

        void Increment(int val);

        void End();
    }

    public enum ProgressAutoCloseTypes
    {
        AutoClose,
        WaitOnEnd,
        WaitOnError
    }

    public class ProgressBarHelper
    {
        public static bool EndProgress(IProgressCallback callback, string message, bool ok)
        {
            if (callback != null)
            {
                if (callback.IsAborting)
                {
                    return false;
                }
                callback.SetText(message, "");
                callback.SetRange(0, 1);
                if (ok)
                {
                    callback.StepTo(1);
                }
                else
                {
                    callback.StepTo(0);
                    callback.AddMessage(ProgressMessageTypes.Critical, "No database schema information found.");
                }
            }
            return true;
        }

        public static bool IncrProgress(IProgressCallback callback, string message, ref int count)
        {
            if (callback != null)
            {
                if (callback.IsAborting)
                {
                    return false;
                }
                callback.SetText(message);
                callback.StepTo(count++);
            }
            return true;
        }

        public static bool StartProgress(IProgressCallback callback, string message, int max, ref int stepto)
        {
            if (max > 0 && callback != null)
            {
                if (callback.IsAborting)
                {
                    return false;
                }
                callback.SetText(message, "");
                callback.SetRange(0, max);
                callback.StepTo(stepto = 0);
            }
            return true;
        }
    }

    public enum ProgressMessageTypes
    {
        Info,
        Warning,
        Critical
    }

}
